package com.ndp.fb.fbagent.service.impl;


import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ndp.fb.fbagent.service.impl.paramadapter.FacebookParamAdapter;
import com.ndp.fb.model.fbagent.param.APIPriority;
import com.ndp.fb.model.fbagent.param.FacebookParam;
import com.ndp.fb.model.fbagent.param.adimage.AdImageParam;
import com.restfb.BinaryAttachment;
import com.restfb.Parameter;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.io.File;
import java.util.*;

/**
 * Created by Mikey.zhaopeng on 2015/3/20 13:50.
 * Email mikey.zhao@ndpmedia.com
 */
public class AdImageFBServiceImplTester {
    @Test
    public void uploadAdImageTest() {
        try {
            Map<String, String> equipment = new HashMap<>();
            equipment.put("entrustAccountId", "1390685454498661");
            equipment.put("accessToken", "actAccountId");
            File file = new File("C:\\Users\\imzhp\\Desktop\\1.jpg");
            byte[] fileByes = SerializationUtils.serialize(file);
            AdImageParam requestParam = new AdImageParam();
            requestParam.setFileBytes(fileByes);
            requestParam.setFileName("default.png");
            requestParam.setApiPriorityCode(APIPriority.UploadImage.getCode());
            requestParam.setEquipment(equipment);
            requestParam.setFbAccountId(Long.parseLong("1390685454498661"));
            this.adapterMap(requestParam);

            Parameter[] parameters = this.adapterArray(requestParam);

            System.out.println(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Test
    public void test() {
        //            String result = adImageFBService.uploadAdImage(this.accountImageUploadBo.getAccessToken(), accountImage.getAccountId(), accountImage, fileByes);
        String result = "{\"images\":{\"default.png\":{\"hash\":\"b9dd17fa199b18a854af8503391a88e0\",\"url\":\"https:\\/\\/fbcdn-creative-a.akamaihd.net\\/hads-ak-xfp1\\/t45.1600-4\\/10737019_6022518294166_826336940_n.png\"}}}";
        JSONObject jsonObject = JSON.parseObject(result);
        Map map = (Map) jsonObject.get("images");
        Set set = map.entrySet();
        Map imageMap = new HashMap();
        for (Iterator it = set.iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry) it.next();
            System.out.println(entry.getKey() + "--->" + entry.getValue());
            JSONObject j1 = JSON.parseObject(entry.getValue().toString());
            String hash = j1.getString("hash");
            if (StringUtils.isNotEmpty(hash)) {
                imageMap.put("hash", hash);
            }
            String url = j1.getString("url");
            if (StringUtils.isNotEmpty(url)) {
                imageMap.put("url", url);
            }
        }
        System.out.println(imageMap);

//            //往FB传返回hash 和fbUrl
//            if (fbAdimage == null || fbAdimage.getImages() == null) {
//                accountImage.setUploadSatus(CommonConstants.ACCOUNT_IMAGE_UPLOAD_STATUS_FAIL);
//                Integer uploadCounts = accountImage.getUploadCounts() + 1;
//                accountImage.setUploadCounts(uploadCounts);
//                accountImage.setUserId(userId);
//                accountImage.setIsNeedUpload(uploadCounts > this.reuploadLimitTimes ? CommonConstants.ACCOUNT_IMAGE_IS_NEED_UPLOAD_NO : CommonConstants.ACCOUNT_IMAGE_IS_NEED_UPLOAD_YES);
//                accountImage.setIsDelete(this.entrustAccountIsDelete(accountImage.getAccountId()));
//                accountImageRdbService.update(accountImage);
//                Logger.info("fbAdimage return is null! accountImageId:" + accountImageId);
//                return false;
//            } else {
//                Integer uploadCounts = accountImage.getUploadCounts() + 1;
//                String fbUrl = "";
//                String fbHash = "";
//                for (Map.Entry<String, FBAdimageDetail> entry : fbAdimage.getImages().entrySet()) {
//                    FBAdimageDetail imgMap = entry.getValue();
//                    fbUrl = imgMap.getUrl();
//                    fbHash = imgMap.getHash();
//                }
//                if (StringUtils.isEmpty(fbUrl) || StringUtils.isEmpty(fbHash)) {
//                    Logger.info("fbUrl or fbHash is empty! accountImageId: accountImageId: " + accountImageId);
//                    return false;
//                }
//                accountImage.setFbUrl(fbUrl);
//                accountImage.setHashCode(fbHash);
//                accountImage.setUploadCounts(uploadCounts);
//                accountImage.setUploadSatus(CommonConstants.ACCOUNT_IMAGE_UPLOAD_STATUS_SUCEESS);
//                accountImage.setIsNeedUpload(CommonConstants.ACCOUNT_IMAGE_IS_NEED_UPLOAD_NO);
//                accountImage.setIsDelete(this.entrustAccountIsDelete(accountImage.getAccountId()));
//                accountImageRdbService.update(accountImage);
//                Logger.info("upload accountImage success! accountImageId: " + accountImageId);
//            }

    }

    protected Parameter[] adapterArray(FacebookParam param) throws Exception {
        List<Parameter> parameters = this.adapter(param);
        Object[] objects = parameters.toArray();
        return parameters.toArray(new Parameter[parameters.size()]);
    }

    protected Map adapterMap(FacebookParam param) throws Exception {
        Map map = new HashMap();
        List params = this.adapter(param);
        if (CollectionUtils.isNotEmpty(params)) {
            List parameters = new ArrayList();
            List binaryAttachments = new ArrayList();
            for (Object object : params) {
                if (object instanceof Parameter) {
                    parameters.add(object);
                } else if (object instanceof BinaryAttachment) {
                    binaryAttachments.add(object);
                }
            }
            map.put("parameters", parameters.toArray());
            map.put("binaryAttachments", binaryAttachments.toArray());
        }
        return map;
    }


    protected List<Parameter> adapter(FacebookParam param) throws Exception {
        return facebookParamAdapter.adapterParam(param);
    }

    FacebookParamAdapter facebookParamAdapter = new FacebookParamAdapter();
}
